//
//  LSMineView.m
//  LookScore
//
//  Created by wr on 2017/12/4.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#define labelWidth self.width/3.0f
#import "LSMineListCell.h"
#import "LSMineView.h"
#import <UIButton+WebCache.h>
#import "LSLoginVController.h"
#import "LSAccountArchive.h"
@interface LSMineView()

@property(strong,nonatomic)UIButton *headImgBu;

@property(strong,nonatomic)UILabel *nameLab;

@property(strong,nonatomic)UIButton *introduceBu;

@property(strong,nonatomic)NSMutableArray *titleArray;

@end

@implementation LSMineView

-(NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray=[[NSMutableArray alloc]init];
    }
    return _titleArray;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        self.backgroundColor=LSBackGroundColor;
        [self addContentView];
        
    }
    return self;
}


-(void)addContentView{
    
   
    UIView *backView=[[UIView alloc]init];
    backView.backgroundColor=LSBackGroundColor;
    [self addSubview:backView];
    backView.sd_layout.leftEqualToView(self).topEqualToView(self).rightSpaceToView(self,0).bottomEqualToView(self);
    
    
    UIImageView *backImageView=[[UIImageView alloc]init];
//    if ([LSAccountArchive account].UserHeadUrl.length == 0) {
        backImageView.image=[UIImage imageNamed:@"MineHead"];
//    }
//    else{
//        backImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[LSAccountArchive account].UserHeadUrl]]];
//    }
    [backView addSubview:backImageView];
    backImageView.userInteractionEnabled=YES;
    backImageView.sd_layout.leftEqualToView(backView).topEqualToView(backView).rightEqualToView(backView).heightIs(self.height);
    
    UIButton *headImgBu=[[UIButton alloc]init];
    [headImgBu setBackgroundImage:[UIImage imageNamed:@"MineDefaultHead"] forState:UIControlStateNormal];
    headImgBu.titleLabel.textColor=LSTitleWhiteColor;
    headImgBu.titleLabel.font=LSFontSize12;
    [headImgBu addTarget:self action:@selector(loginOrReplacePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:headImgBu];
    self.headImgBu=headImgBu;
    headImgBu.sd_layout.centerXEqualToView(backImageView).topEqualToView(backImageView).offset(40).widthIs(80).heightIs(80);
    
    UILabel *nameLab=[[UILabel alloc]init];
    nameLab.font=LSFontSize15;
    nameLab.textColor=LSTitleWhiteColor;
    nameLab.textAlignment=NSTextAlignmentCenter;
    [backImageView addSubview:nameLab];
    self.nameLab=nameLab;
    nameLab.sd_layout.leftEqualToView(backImageView).topSpaceToView(headImgBu,5).rightEqualToView(backImageView).heightIs(20);
    
    UIButton *introduceBu=[[UIButton alloc]init];
    introduceBu.titleLabel.font=LSFontSize12;
    introduceBu.titleLabel.numberOfLines = 2;
    introduceBu.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [introduceBu setTitleEdgeInsets:UIEdgeInsetsMake(10, 30, 10, 10)];
    [introduceBu setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    introduceBu.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [introduceBu setImage:[UIImage imageNamed:@"MineEdit"] forState:UIControlStateNormal];
//    introduceBu.titleEdgeInsets=UIEdgeInsetsMake(0, 0, 0, -20);
    [introduceBu addTarget:self action:@selector(editIntroduce:) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:introduceBu];
    self.introduceBu=introduceBu;
    introduceBu.sd_layout.leftEqualToView(backImageView ).topSpaceToView(nameLab,5).rightEqualToView(backImageView).heightIs(20);
    
    
    for (int i=0; i<3; i++) {
        
        UILabel *label=[[UILabel alloc]init];
        label.font=LSFontSize13;
        label.textColor=LSTitleWhiteColor;
        label.numberOfLines=0;
        label.textAlignment=NSTextAlignmentCenter;
        [backImageView addSubview:label];
        label.sd_layout.leftEqualToView(backImageView).offset(labelWidth*i).topSpaceToView(introduceBu, 10).bottomSpaceToView(backImageView, 10).widthIs(labelWidth).heightIs(33);
        [self.titleArray addObject:label];
        if (i<2) {
            UIView *lineView=[[UIView alloc]init];
            lineView.backgroundColor=LSTitleWhiteColor;
            [label addSubview:lineView];
            lineView.sd_layout.rightEqualToView(label).topEqualToView(label).widthIs(1).bottomEqualToView(label);
        }
       
    }
    
    [self addControlData];
  
}

-(void)addControlData{
    //未登录//
    if (![LSAccountArchive isLogin]) {
        [self.headImgBu setBackgroundImage:nil forState:UIControlStateNormal];
        
        [self.headImgBu setBackgroundImage:[UIImage imageNamed:@"MineDefaultHead"] forState:UIControlStateNormal];
        
        [self.headImgBu setTitle:@"登录/注册" forState:UIControlStateNormal];
        
        self.nameLab.text=@"";
       
        [self.introduceBu setTitle:@"请登录" forState:UIControlStateNormal];
        
        [self.titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel *label=(UILabel *)obj;
            if (idx==0) {
                label.text=@"0\n话题";
                
            }else if (idx==1){
                label.text=@"0\n关注";
            }else{
                label.text=@"0\n粉丝";
            }
        }];
        
    }else{
       [self.headImgBu setTitle:@"" forState:UIControlStateNormal];
        
//        if ([LSAccountArchive account].UserHeadUrl.length == 0) {
//            [self.headImgBu setBackgroundImage:[UIImage imageNamed:@"MineDefaultHead"] forState:UIControlStateNormal];
//        }else{
        [self.headImgBu sd_setBackgroundImageWithURL:[NSURL URLWithString:[LSAccountArchive account].UserHeadUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"MineDefaultHead"] options:SDWebImageRefreshCached];
//        }
        
        self.nameLab.text=[NSString stringWithFormat:@"%@",[LSAccountArchive account].DisplayName];
        
        if ([LSAccountArchive account].SpecialWords.length == 0) {
            [self.introduceBu setTitle:@"这个人有点懒什么也没留下" forState:UIControlStateNormal];
        }else{
            [self.introduceBu setTitle:[LSAccountArchive account].SpecialWords forState:UIControlStateNormal];
        }
        
        
        [self.titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel *label=(UILabel *)obj;
            if (idx==0) {
                label.text=[NSString stringWithFormat:@"%@\n话题",[LSAccountArchive account].TopicPublishCount];
                MyLog(@"1 -- %@",[LSAccountArchive account].TopicPublishCount);
                
            }else if (idx==1){
                label.text=[NSString stringWithFormat:@"%@\n关注",[LSAccountArchive account].AttentionsCount];
                MyLog(@"2 -- %@",[LSAccountArchive account].AttentionsCount);
            }else{
                label.text=[NSString stringWithFormat:@"%@\n粉丝",[LSAccountArchive account].FansCount];
                MyLog(@"3 -- %@",[LSAccountArchive account].FansCount);
            }
        }];
    }
    
    
    
    
}

#pragma mark 🔥 loginOrReplacePhotoAction

-(void)loginOrReplacePhotoAction:(UIButton *)sender{
    
    if (self.loginOrReplacePhotoBlock) {
        
        if (![LSAccountArchive isLogin]){
            self.loginOrReplacePhotoBlock(KLoginOrReplacePhotoIsLogin);
        }else{
          self.loginOrReplacePhotoBlock(KLoginOrReplacePhotoIsReplace);
        }
        
    }
    
    
}
- (void)editIntroduce:(UIButton *)sender{
    if (self.editWordsBlock) {
        self.editWordsBlock();
    }
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
